package com.chahu.servlet.front;

import com.alibaba.fastjson2.JSONObject;
import com.chahu.entity.OrderDetail;
import com.chahu.service.DetailService;
import com.chahu.service.impl.DetailServiceImpl;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/detail")
public class DetailServlet extends HttpServlet {

    DetailService detailService;
    @Override
    public void init() throws ServletException {
        detailService = new DetailServiceImpl();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=uft-8");

        String json = IOUtils.toString(req.getReader());

        JSONObject data = JSONObject.parseObject(json);
        String imgUrl = data.getString("imgUrl");
        String proName = data.getString("proName");
        Integer proPrice = Integer.parseInt(data.getString("proPrice"));

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setName(proName);
        orderDetail.setImage(imgUrl);
        orderDetail.setPrice(proPrice);
        orderDetail.setCategoryId(1);
        detailService.insertDetail(orderDetail);
    }
}
